﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using estudent.Models;

namespace estudent.Controllers
{
    public class IzvajalciUredController : Controller
    {
        estudentEnt db = new estudentEnt();
        //
        // GET: /IzvajalciUred/

        public ActionResult Index()
        {

            return View(db.Poucujes.OrderBy(p => p.sPredmeti.imePredmeta).ThenBy(p => p.SProfesorji1.priimekProfesor).ThenBy(p => p.SProfesorji2.priimekProfesor).ThenBy(p => p.SProfesorji.priimekProfesor).ToList());
        }

        //
        // GET: /IzvajalciUred/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /IzvajalciUred/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /IzvajalciUred/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /IzvajalciUred/Edit/5
 
        public ActionResult Edit(int id)
        {
            var uredi = (from p in db.Poucujes
                         where p.idPoucuje == id
                         select p).First();

            return View(uredi);
        }

        //
        // POST: /IzvajalciUred/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /IzvajalciUred/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /IzvajalciUred/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var orig = (from p in db.Poucujes
                            where p.idPoucuje == id
                            select p).First();
                var spremen = (from p in db.Poucujes
                               where p.idPoucuje == id
                               select p).First();

                if (orig.veljavnost != null)
                {
                    spremen.veljavnost = !orig.veljavnost;
                }
                else
                {
                    spremen.veljavnost = false;
                }

                db.ApplyCurrentValues(orig.EntityKey.EntitySetName, spremen);
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
